<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="form">
          <el-form-item prop="name" label="资源名称">
            <el-input v-model="formInline.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径">
            <el-input v-model="formInline.url" placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类">
            <el-select v-model="formInline.categoryId" placeholder="资源分类" clearable>
              <el-option
                v-for="item in cateDate"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" type="primary" @click="onSubmit" :disabled="isLoadings">查询搜索</el-button>
            <el-button size="mini" @click="onReset" :disabled="isLoadings">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <span class="span_btn">
        <el-button size="small" @click="onAddResource">添加</el-button>
        <el-button size="small" @click="onStyleResource">资源分类</el-button>
      </span>
      <el-table
        :data="resoureData"
        border
        style="width: 100%"
        v-loading="isLoadings">
        <el-table-column
          prop="id"
          label="编号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="资源名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="url"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="添加时间">
        </el-table-column>
        <el-table-column
          label="操作">
           <template slot-scope="scope">
             <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
           </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--
      total:总记录数
      pagesize：每页大小,必须存在 page-sizes内
      组件根据 total和 pagesize 自动计算分多少页
     -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="formInline.current"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="formInline.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :disabled="isLoadings">
    </el-pagination>

    <add-resource
      ref="handlerAddResource"
      :dialogVisible="dialogVisible"
      :isEdit = "isEdit"
      :currentData="currentData"
      @watchChild="refreshPage"
      />
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import AddResource from '@/views/resource/components/AddRes.vue'
import { deleteRes, getAllCategory, getResourcePages, ResAdd } from '@/services/resource'
import { Form } from 'element-ui'
export default Vue.extend({
  name: 'SourceList',
  components: {
    AddResource
  },
  data () {
    return {
      resoureData: [], // 资源列表
      cateDate: [], // 资源分类列表
      formInline: { // 筛选表单
        name: '',
        url: '',
        current: 1, // 默认查询第一页
        size: 5, // 每页大小
        categoryId: null // 资源分类ID
      },
      totalCount: 0, // 资源条数
      dialogVisible: false, // 添加编辑 页是否显示
      isEdit: false, // 区分 添加，编辑
      currentData: {}, // 编辑项数据
      isLoadings: true
    }
  },
  methods: {
    // 获取所有资源请求
    async handlerAllResource () {
      this.isLoadings = true
      // const { data } = await getAllResource()
      const { data } = await getResourcePages(this.formInline)
      // console.log('分页查', data)
      if (data.code === '000000') {
        this.resoureData = data.data.records
        this.totalCount = data.data.total
        this.isLoadings = false
      }
    },
    // 获取资源分类
    async handlerAllCate () {
      const { data } = await getAllCategory()
      // console.log('资源分类', data)
      if (data.code === '000000') {
        this.cateDate = data.data
      }
    },
    // 添加资源
    onAddResource () {
      this.dialogVisible = true
      this.isEdit = false
      // (this.$refs.handlerAddResource as any).recordResourceBol(this.dialogVisible)
    },
    // 编辑资源
    handleEdit (row:ResAdd) {
      this.dialogVisible = true
      this.isEdit = true
      this.currentData = row
    },
    // 删除资源
    handleDelete (row:ResAdd) {
      // console.log(data)
      this.$confirm('此操作将删除一项分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteRes(row.id as number)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.handlerAllResource()
        }
      }).catch(() => {
        this.$message.error('删除失败')
      })
    },
    // 资源分类
    onStyleResource () {
      this.$router.push('/resourceCategory')
    },
    // 刷新页面
    refreshPage () {
      this.dialogVisible = false
      this.handlerAllResource()
    },
    // 搜索查询
    onSubmit () {
      // console.log('submit!')
      this.formInline.current = 1 // 筛选查询从第一页开始
      this.handlerAllResource()
    },
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.formInline.current = 1
      this.handlerAllResource()
    },
    // handleEdit (data) {
    //   this.dialogVisible = true;
    //   (this.$refs.handlerAddResource as any).recordResourceBol(this.dialogVisible)
    // },
    // handleDelete () {

    // }
    handleSizeChange (val:number) {
      // console.log(`每页 ${val} 条`)
      this.formInline.size = val
      this.formInline.current = 1 // 每页大小改变重新查询第一页
      this.handlerAllResource()
    },
    handleCurrentChange (val:number) {
      // console.log(`当前页: ${val}`)
      this.formInline.current = val // 修改页码
      this.handlerAllResource()
    }
  },
  created () {
    this.handlerAllResource()
    this.handlerAllCate()
  }
})
</script>

<style scoped lang="scss">
.span_btn{
  display: inline-block;
  margin-bottom: 2rem;
}
</style>
